Skip to content

Alert user of database table permission errors during search-replace#216

Open
Copilot wants to merge 4 commits intomainfrom
copilot/alert-user-db-permissions-errors
Open

Alert user of database table permission errors during search-replace#216
Copilot wants to merge 4 commits intomainfrom
copilot/alert-user-db-permissions-errors

Conversation

Copy link
Contributor

Copilot AI commented Mar 15, 2026

When a database UPDATE fails (e.g. read-only table, insufficient permissions), search-replace silently reported 0 replacements with no indication of why — even after a dry-run showing expected changes.

Changes

  • SQL path (sql_handle_col): Check $wpdb->last_error after $wpdb->query() UPDATE calls; emit WP_CLI::warning() with the table name, column name, and the raw database error.
  • PHP path (php_handle_col): Same check after each $wpdb->update() call.

Example output when a table is not writable:

Warning: Error updating column 'post_content' in table 'wp_posts': UPDATE command denied to user 'wpuser'@'localhost' for table 'wp_posts'
  • Behat test: Covers the SQL path by creating a table with a BEFORE UPDATE trigger that raises a MySQL signal error, verifying that STDERR contains the expected warning with the column and table name.
Original prompt

This section details on the original issue you should resolve

<issue_title>Alert the user of db table permissions errors</issue_title>
<issue_description>## Feature Request

Use Case and user story

When there are database table permissions errors, search-replace should be able to alert the user.

I was working with an older DB that I had manually imported the wp_posts table with the wrong DB permissions. (the table was read-only)

When I did a dry-run of the search-replace it worked great, showed me all the replacements to be made, etc.

Then when I did a live run, it went through the process and still reported that there were 527 changes to make, but instead of making them it returned Success: 0 Replacements made

I downloaded a more blunt tool for forcing the DB updates through, and that's when I discovered that the table in question is read-only. Once I knew that, the workaround was simple: export the DB to a fresh DB using wp db export and then import that fresh database, which resets the permissions, then search-replace worked like a charm.

I also tried with --verbose and --debug but the critical piece (when the actual SQL is running) is not outputting anything useful.

Output of the Dry Run

527 Replacements to be made

Output of the --debug argument on a live run

Success: made 0 replacements

Proposed solution

It would be more helpful if table permission errors are preventing the actual changing, that this is output.

I'd propose an additional argument of --errors or adding this output to the existing --debug argument.
</issue_description>

Comments on the Issue (you are @copilot in this section)

@schlessera Thanks for the report, @Benunc! I think it might make even more sense to actually forward errors or warnings, and in this case show an error that a read-only db/table cannot be changed.

✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>
Copilot AI changed the title [WIP] Add alert for database table permissions errors Alert user of database table permission errors during search-replace Mar 15, 2026
Copilot AI requested a review from swissspidy March 15, 2026 07:51
@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

❌ Patch coverage is 53.33333% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/Search_Replace_Command.php 53.33% 7 Missing ⚠️

📢 Thoughts on this report? Let us know!

@swissspidy swissspidy marked this pull request as ready for review March 15, 2026 08:21
@swissspidy swissspidy requested a review from a team as a code owner March 15, 2026 08:21
Copilot AI review requested due to automatic review settings March 15, 2026 08:21

This comment was marked as resolved.

Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
@swissspidy
Copy link
Member

@copilot failing test:


001 Scenario: Warn when updating a table fails due to a database error # features/search-replace.feature:1579
      Then STDERR should contain:                                      # features/search-replace.feature:1586
        $ wp search-replace old-value new-value wp_readonly_test
        
        Error: Couldn't find any tables matching: wp_readonly_test
        cwd: /tmp/wp-cli-test-run--69b7c519c30eb4.40486879/
        run time: 0.30239796638489
        exit status: 1
        
        --- Expected
        +++ Actual
        @@ @@
        -Error updating column 'data' in table 'wp_readonly_test'
        +Error: Couldn't find any tables matching: wp_readonly_test
         (Exception)

…ble name

Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com>

This comment was marked as resolved.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Alert the user of db table permissions errors

3 participants